package hw;

public class Solution07 {

    public int myReverse(int x) {
        if (x == 0) {
            return 0;
        }
        String s = x + "";
        char[] chars = s.toCharArray();
        StringBuilder stringBuffer = new StringBuilder(32);
        for (int i = 0; i < chars.length; i++) {
            if(x < 0 && i == 0){
                continue;
            }
            stringBuffer.append(chars[i]);
        }
        long longR = Long.parseLong(stringBuffer.reverse().toString());
        longR = x > 0 ? longR : -longR;
        return (longR > Integer.MAX_VALUE || longR < Integer.MIN_VALUE) ? 0 : (int)longR;
    }
    public int reverse(int x) {
        if(x == 0){
            return 0;
        }
        long result = 0;
        while (x != 0){
            result = result *10 + x % 10;
            x /= 10;
        }
        if(x < 0){
            result = - result;
        }
        if(result > Integer.MAX_VALUE || result <Integer.MIN_VALUE){
            return 0;
        }
        return (int)result;
    }

    public static void main(String[] args) {
        Solution07 solution07 = new Solution07();
        System.err.println(solution07.reverse(754678));
    }
}
